package com.uchedao.seller.ui.province_city;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.uchedao.seller.R;
import com.uchedao.seller.db.CarDbHelper;
import com.uchedao.seller.inf.ICityResultBack;
import com.uchedao.seller.model.CityEntity;
import com.uchedao.seller.model.ProvinceEntity;
import com.uchedao.seller.ui.baidu.BaseFragForBaidu;
import com.uchedao.seller.util.LogUtil;

import java.util.ArrayList;

/**
 * 城市选择界面
 * Created by ZhangZhanghua on 2015/10/29 0029.
 */
public class ProvinceCityFragment extends BaseFragForBaidu {
    private ListView provinceView;
    private ListView cityView;
    private ProvinceAdapter provinceAdapter;
    private CityAdapter cityAdapter;
    private ArrayList<ProvinceEntity> provinceList;
    private ArrayList<CityEntity> cityList;
    private int provinceId, cityId;
    private String provinceName, cityName;

    private int province_position = 0;

    private static ICityResultBack iCityResultBack;
    public static final String EXTRA_LISTENER = "extra_listener";

    private static BaseFragForBaidu mInstance = null;

    /**
     * 获取单例
     */
    public static synchronized BaseFragForBaidu getInstance(ICityResultBack cityResultBack) {
        if (mInstance == null) {
            mInstance = new ProvinceCityFragment();
            iCityResultBack = cityResultBack;
            LogUtil.e("ProvinceCityFragment");
        }
        return mInstance;
    }

    @Override
    protected String getActivityTag() {
        return "ProvinceCityFragment";
    }

    @Override
    public int getViewLayoutId() {
        return R.layout.fragment_province_city;
    }

    @Override
    public void initListener() {
        provinceView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                province_position = position;
                provinceId = provinceList.get(position).getId();
                provinceName = provinceList.get(position).getName();
                cityList = CarDbHelper.getCity(getActivity(), provinceId);
                cityAdapter = new CityAdapter(getActivity(), cityList, provinceName);
                cityView.setAdapter(cityAdapter);
                cityAdapter.notifyDataSetChanged();
                cityView.setVisibility(View.VISIBLE);
            }
        });
        cityView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityId = cityList.get(position).getId();
                cityName = cityList.get(position).getName();
                if (iCityResultBack != null) {
                    LogUtil.e("iCityResultBack");
                    iCityResultBack.onCityResult(provinceList.get(province_position), cityList.get(position));
                    toBack(mInstance);
                }
            }
        });
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            this.iCityResultBack = (ICityResultBack) getArguments().getSerializable(EXTRA_LISTENER);
        }
        provinceList = CarDbHelper.getProvince(getActivity());
        provinceAdapter = new ProvinceAdapter(getActivity(), provinceList);
        provinceView.setAdapter(provinceAdapter);

    }

    @Override
    public void initView() {
        provinceView = (ListView) findViewById(R.id.province_list);
        cityView = (ListView) findViewById(R.id.city_list);
    }

}
